<html>
<head><meta charset="utf-8"><title>Bounds on non-type-parameters work, is that on purpose? · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html">Bounds on non-type-parameters work, is that on purpose?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222214631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222214631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222214631">(Jan 10 2021 at 09:43)</a>:</h4>
<p>I've noticed that this code compiles in every single Rust version that allows user-defined const fns:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">Foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span>: <span class="nb">Clone</span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Was this supposed to compile before const bounds are stabilized?</p>



<a name="222214743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222214743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222214743">(Jan 10 2021 at 09:46)</a>:</h4>
<p>Just the bound, without actually using anything from the trait?  Seems low-risk, at least...</p>



<a name="222214757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222214757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222214757">(Jan 10 2021 at 09:47)</a>:</h4>
<p>I discovered this behavior when trying to use an associated constant from a trait in a const fn.<br>
I haven't written any committed code yet that relies on this working.</p>
<p>Here's an example of the const method that uses an associated constant being called at compile-time on stable Rust:<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19592d24da0e3a6c00987e4030c98648">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19592d24da0e3a6c00987e4030c98648</a><br>
With the possibly problematic impl being:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="bp">Self</span>: <span class="nc">Trait</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// Gets the usize value of this type</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">to_usize</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&lt;</span><span class="bp">Self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">Trait</span><span class="o">&gt;</span>::<span class="n">USIZE</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222279389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222279389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222279389">(Jan 11 2021 at 10:20)</a>:</h4>
<p>ugh... this is indeed a forward compat issue. We wanted to catch this, not sure how we failed to do that... The problem is not usage within the const fn, but usage <em>of</em> the const fn. Right now it is allowed, but it will fail to compile in the future if used with types that do not satisfy <code>Self: const Trait</code></p>



<a name="222279393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222279393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222279393">(Jan 11 2021 at 10:20)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=db88cc5820ed910400af3dc694aa5aa9">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=db88cc5820ed910400af3dc694aa5aa9</a></p>



<a name="222279409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222279409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222279409">(Jan 11 2021 at 10:20)</a>:</h4>
<p>we may have painted ourselves into a corner</p>



<a name="222284222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222284222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222284222">(Jan 11 2021 at 11:12)</a>:</h4>
<p>fix it and do a crater run to valuate the fallout?</p>



<a name="222284260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222284260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222284260">(Jan 11 2021 at 11:13)</a>:</h4>
<p>OTOH, requiring <code>const</code> bounds explicitly to make functions callable would anyway be more consistent with <code>fn</code> pointers and <code>dyn Trait</code> objects ;)</p>



<a name="222285131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222285131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222285131">(Jan 11 2021 at 11:22)</a>:</h4>
<p>yeaa</p>



<a name="222285133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222285133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222285133">(Jan 11 2021 at 11:23)</a>:</h4>
<p>I know <span aria-label="frown" class="emoji emoji-1f641" role="img" title="frown">:frown:</span></p>



<a name="222285149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222285149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222285149">(Jan 11 2021 at 11:23)</a>:</h4>
<p>I think we'll have to got down that road</p>



<a name="222354846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Bounds%20on%20non-type-parameters%20work%2C%20is%20that%20on%20purpose%3F/near/222354846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Bounds.20on.20non-type-parameters.20work.2C.20is.20that.20on.20purpose.3F.html#222354846">(Jan 11 2021 at 20:37)</a>:</h4>
<p>Lets fix that in the 2021 edition</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>